package com.ifengchao.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ifengchao.dao.iface.SolrSearchUserDao;
import com.ifengchao.pojo.HUser;
import com.ifengchao.service.SolrUserSearchService;

/**
 * 搜索实现User
 *
 * @author loki.xu
 * @version V1.0	
 * 
 * @date 2018年1月9日 下午2:28:12
 */
@Service
public class SolrUserSearchServiceImpl implements SolrUserSearchService {

	private static final Logger logger = Logger.getLogger(SolrUserSearchServiceImpl.class);
	
	@Autowired(required = true)
	private SolrSearchUserDao solrSearchUserDao;
	
	@Autowired(required = true)
	private SolrServer solrServer;
	
	@Override
	public void pushUserSolrSearch() {
		// 是否要check开关
		
		try {
			List<HUser> userList = new ArrayList<HUser>();
			userList = solrSearchUserDao.getUserList();
			System.out.println("pushUserSolrSearch userList:" + userList.size());
			logger.info("pushUserSolrSearch userList:" + userList.size());
			
			if (userList != null && userList.size() > 0) {
				for (int i = 0; i < userList.size(); i++) {
					System.out.println(userList.get(i).getUserId());
					logger.info("pushUserSolrSearch userId:" + userList.get(i).getUserId());
					
					// 先删除原来已存在solr的用户
					solrServer.deleteById("U" + userList.get(i).getUserId());
					solrServer.commit();
					
					// 重新推送
					SolrInputDocument document = new SolrInputDocument();
					document.addField("id", "U" + userList.get(i).getUserId());
					document.addField("nickname", userList.get(i).getNickName());
					document.addField("user_portrait", userList.get(i).getUserPortrait());
					document.addField("user_id", userList.get(i).getUserId());
					document.addField("select_type", "user");
					
					solrServer.add(document);
					
					solrServer.commit();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("pushUserSolrSearch Exception:" + e);
		}
		System.out.println("pushUserSolrSearch 正常结束");
		logger.info("pushUserSolrSearch 正常结束");
	}

}
